<?php
class DorimeblogController extends  AppController{
	var $name = 'DorimeBlog' ;
	var $uses = null ;
	var $layout = 'dorime_blog';
	
	
	//This one is for user to login his blog.
	function index( $blogId = null ){		
		$this->set('title_for_layout', 'Dorime');
		// if the blog'id is null ,we use the first blog as the default blog.
		if(empty($blogId)){
			$email = $this->Auth->user('email');
			if(!empty($email)){
				// if the arguments $blogId is null ,we find the logined user and its blog
				$this->loadModel('UsersBlog');
				$this->UsersBlog->unBindModel(array('belongsTo'=>array('Blog'))); 
				$result = $this->UsersBlog->find('first',array(
					'conditions' => array( 'User.email'=>$email ),
					'fields'=>array('UsersBlog.users_id','UsersBlog.blogs_id'),
					'order'=>'User.id desc'
				));
				$blogId = $result['UsersBlog']['blogs_id'] ;
			}
		}
		// find the data of  the blog according to its blog id .
		$this->loadModel('UsersBlog');
		$blogIdInfo = $this->UsersBlog->find('first',array(
			'conditions'=>array('Blog.id'=>$blogId),
			'fields'=>array('Blog.id','Blog.title','Blog.url','Blog.followed_count','User.id','User.following_count')
		));
	 	$this->set('followed',$blogIdInfo['Blog']['followed_count']);
	 	$this->set('following',$blogIdInfo['User']['following_count']) ;
	 	// find the posts who created by the blog.
	 	$this->loadModel('Post');
	 	$posts = $this->Post->find('all',array(
	 		'conditions'=>array('Post.blogs_id'=>$blogIdInfo['Blog']['id'],'Post.status'=>'Published'),
	 		'fields'=>array('Post.id','Post.title','Post.reblog_posts_id','Post.excerpt','Post.blogs_id','Post.response_count','Post.reblog_posts_id'),
	 		'limit'=>5,
	 		'order'=>'Post.id desc'
	 	));
	 	//set the blog icon ,find the post's tags ,and insert them into the posts variable ,
	 	//and find if the user like the post or reblog the post
	 	$this->loadModel('PostHasTag');
	 	$this->loadModel('Response') ;
	 	$this->PostHasTag->unBindModel( array( 'belongsTo' => array('Post') ));
	 	foreach($posts as $key => $value ){
	 		// find the resoponse count of the post .
	 		if(!empty( $posts[$key]['Post']['reblog_posts_id']) ){
	 			$orPost = $this->Post->find('first',array(
	 					'conditions'=>array('Post.id'=>$posts[$key]['Post']['reblog_posts_id']),
	 					'fields'=>array('Post.response_count')
	 			));
	 			if(!empty($orPost)){
	 				$posts[$key]['Post']['response_count'] = $orPost['Post']['response_count'] ;
	 			}
	 		}
	 		// set the blog icon 
	 		$posts[$key]['Post']['blog_icon'] = '' ;
	 		// find the post 's tag.
		 	$tags = $this->PostHasTag->find('all',array(
		 		'conditions'=>array('PostHasTag.posts_id'=>$value['Post']['id']),
		 		'fields'=>array('Tag.id' ,'Tag.name')
		 	));
		 	$i = 0 ;
		 	$posts[$key]['Post']['tags'][$i]['name'] = null ;
		 	$posts[$key]['Post']['tags'][$i]['id'] = null ;
		 	foreach ($tags as $key1 => $value1 ){
		 		$posts[$key]['Post']['tags'][$i]['name'] = $value1['Tag']['name'] ;
		 		$posts[$key]['Post']['tags'][$i]['id']	 = $value1['Tag']['id'] ;
		 		++$i ;
		 	}
		 	// find whether the current user like the post 
		 	$responseType = $this->Response->find('first',array(
		 		'conditions'=>array('Response.users_id'=>$blogIdInfo['User']['id'],'Response.posts_id'=>$posts[$key]['Post']['id']),
		 		'fields'=>array('Response.type','Response.date')
		 	));
		 	 
		 	if($responseType['Response']['type'] == 'like' ){
		 		$posts[$key]['Post']['like'] = 1 ;
		 	}else 
		 		$posts[$key]['Post']['like'] = 0 ;
		 	if($responseType['Response']['type'] == 'reblog'){
		 		$posts[$key]['Post']['reblog'] = 1 ;
		 	}else {
		 		$posts[$key]['Post']['reblog'] = 0 ;
		 	}
	 	}
	 	// set the posts to the view
	 	$this->set('posts',$posts);
	 	$this->set('blogId',$blogId);
	 	
		$cookie = $this->Cookie->read('Auth.User');
		$userName = $cookie['username'];
	 	$this->set('username',$userName);
	}
	
	function settings( $blogId = null ){	
	 	$this->set('blogId',$blogId);	
		$this->set('title_for_layout', 'Settings | Dorime');// find the data of  the blog according to its blog id .
		
		$this->loadModel('UsersBlog');
		$blogIdInfo = $this->UsersBlog->find('first',array(
			'conditions'=>array('Blog.id'=>$blogId),
			'fields'=>array('Blog.id','Blog.title','Blog.url','Blog.followed_count','User.id','User.following_count')
		));
	 	$this->set('followed',$blogIdInfo['Blog']['followed_count']);
	}
	
	function followers($blogId = null){	
		$this->layout = null;
		
		$this->LoadModel('Blog');
		$followerNum = $this->Blog->find('all',array(
				'conditions'=>array(
					'Blog.id'=>$blogId
				 ),
				'fields'=>array('Blog.followed_count')
		));
		$this->set('followerNum',$followerNum[0]['Blog']['followed_count']);
		
		$blogs = $this->UsersBlog->find('all',array(
				'conditions'=>array(
					'UsersBlog.blogs_id'=>$blogId,
					'UsersBlog.status'=>'follow'
				 ),
				'fields'=>array('User.username')
			));
		
		$this->set('blogs',$blogs);	
	}
	
	function drafts($blogId = null){
		$this->layout = null;
		$this->set('title_for_layout', 'Drafts | Dorime');
		
		// if the blog'id is null ,we use the first blog as the default blog.
		if(empty($blogId)){
			$email = $this->Auth->user('email');
			if(!empty($email)){
				// if the arguments $blogId is null ,we find the logined user and its blog
				$this->loadModel('UsersBlog');
				$this->UsersBlog->unBindModel(array('belongsTo'=>array('Blog'))); 
				$result = $this->UsersBlog->find('first',array(
					'conditions' => array( 'User.email'=>$email ),
					'fields'=>array('UsersBlog.users_id','UsersBlog.blogs_id'),
					'order'=>'User.id desc'
				));
				$blogId = $result['UsersBlog']['blogs_id'] ;
			}
		}
		// find the data of  the blog according to its blog id .
		$this->loadModel('UsersBlog');
		$blogIdInfo = $this->UsersBlog->find('first',array(
			'conditions'=>array('Blog.id'=>$blogId),
			'fields'=>array('Blog.id','Blog.title','Blog.url','Blog.followed_count','User.id','User.following_count')
		));
	 	$this->set('followed',$blogIdInfo['Blog']['followed_count']);
	 	$this->set('following',$blogIdInfo['User']['following_count']) ;
	 	// find the posts who created by the blog.
	 	$this->loadModel('Post');
	 	$posts = $this->Post->find('all',array(
	 		'conditions'=>array('Post.blogs_id'=>$blogIdInfo['Blog']['id'],'Post.status'=>'Draft'),
	 		'fields'=>array('Post.id','Post.title','Post.reblog_posts_id','Post.excerpt','Post.blogs_id','Post.response_count'),
	 		'limit'=>5,
	 		'order'=>'Post.id desc'
	 	));
	 	//set the blog icon ,find the post's tags ,and insert them into the posts variable ,
	 	//and find if the user like the post or reblog the post
	 	$this->loadModel('PostHasTag');
	 	$this->loadModel('Response') ;
	 	$this->PostHasTag->unBindModel( array( 'belongsTo' => array('Post') ));
	 	foreach($posts as $key => $value ){
	 		// set the blog icon 
	 		$posts[$key]['Post']['blog_icon'] = '' ;
	 		// find the post 's tag.
	 		$value['Post']['id'] ;
		 	$tags = $this->PostHasTag->find('all',array(
		 		'conditions'=>array('PostHasTag.posts_id'=>$value['Post']['id']),
		 		'fields'=>array('Tag.id' ,'Tag.name')
		 	));
		 	$i = 0 ;
		 	foreach ($tags as $key1 => $value1 ){
		 		$posts[$key]['Post']['tags'][$i]['name'] = $value1['Tag']['name'] ;
		 		$posts[$key]['Post']['tags'][$i]['id']	 = $value1['Tag']['id'] ;
		 		++$i ;
		 	}		 	
	 	}
	 	// set the posts to the view
	 	$this->set('posts',$posts);
	 	$this->set('blogId',$blogId);
	}
}
	
?>